#import "HttpRequestParamEnginner.h"
#import "SystemUtil.h";
#import "StringUtil.h"
#import "tinyxml.h"
#import "AlertViewEnginner.h"
static CHttpRequestParamEnginner* iInstance=Nil;

@implementation CHttpRequestParamEnginner



-init
{
	if(self=[super init])
	{
		iParam=[[CHttpRequestParam alloc] init];
		iTempParam=[[CHttpRequestParam alloc] init];
		return self;
	}
	return nil;
}

-(void)InitCHttpRequestParam
{
	iConfigPath=[SystemUtil InitFileConfig:@"config.xml"];
	NSString *textString;
	TiXmlDocument doc;
	doc.LoadFile([CStringUtil TransformNSStringToChar:iConfigPath]);
	TiXmlElement* RootElement = doc.RootElement();
	if(RootElement)
	{
		TiXmlElement *item=RootElement->FirstChildElement();
		const char *name;
		const char *text;
		while(item)
		{
			name=item->Value();
			text=item->GetText();
			textString=[CStringUtil TransformCharToNSString:(char*)text];
			if(strcmp(name,"DateTime")==0)
			{
				iParam.iBirthday=textString;
			}else if(strcmp(name,"Sex")==0)
			{
				iParam.iSex=textString;
			}else if(strcmp(name,"ProvinceId")==0)
			{
				iParam.iProvinceId=textString;
			}else if(strcmp(name,"Province")==0)
			{
				iParam.iProvince=textString;
			}else if(strcmp(name,"CityId")==0)
			{
				iParam.iCityId=textString;
			}else if(strcmp(name,"City")==0)
			{
				iParam.iCity=textString;
			}else if(strcmp(name,"ZoneId")==0)
			{
				iParam.iZoneId=textString;
			}else if(strcmp(name,"Zone")==0)
			{
				iParam.iZone=textString;
			}
			
			item=item->NextSiblingElement();
		}
		
	}
}

-(void)SaveHttpRequestParam
{
	TiXmlText *TextNode;
	const char *data;
	TiXmlElement *element;
	TiXmlDocument *doc=new TiXmlDocument;
	TiXmlElement *rootElement = new TiXmlElement("ConfigRoot");
	doc->LinkEndChild(rootElement);
	element= new TiXmlElement("DateTime");
	data=[CStringUtil TransformNSStringToChar:iParam.iBirthday];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("Sex");
	data=[CStringUtil TransformNSStringToChar:iParam.iSex];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("ProvinceId");
	data=[CStringUtil TransformNSStringToChar:iParam.iProvinceId];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("Province");
	data=[CStringUtil TransformNSStringToChar:iParam.iProvince];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("CityId");
	data=[CStringUtil TransformNSStringToChar:iParam.iCityId];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("City");
	data=[CStringUtil TransformNSStringToChar:iParam.iCity];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("ZoneId");
	data=[CStringUtil TransformNSStringToChar:iParam.iZoneId];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	element= new TiXmlElement("Zone");
	data=[CStringUtil TransformNSStringToChar:iParam.iZone];
	TextNode=new TiXmlText(data);
	free((void*)data);
	element->LinkEndChild(TextNode);
	rootElement->LinkEndChild(element);
	doc->SaveFile([CStringUtil TransformNSStringToChar:iConfigPath]);
	delete doc;
	
}
-(void)dealloc
{
	if(iTempParam)
	{
		[iTempParam release];
	}
	if(iParam)
	{
		[iParam release];
	}
	
	[iConfigPath release];
	[super dealloc];
}

+ (CHttpRequestParamEnginner*)GetIntance
{
	
	@synchronized(self)
	{
		if(!iInstance)
		{
			iInstance=[[CHttpRequestParamEnginner alloc] init];
		}
	}
	return iInstance;
}

+ (void)Destroy
{
	@synchronized(self)
	{
		if(iInstance)
		{
			[iInstance release];
		}
	}
}
@end



